﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;
using ElementLib.Interface;
using System.Windows.Forms;
using HalconDotNet;
using System.Collections;

namespace ElementLib
{
    public class CirclePack:IElemPack
    {
        private Circle circle;
        private ROI detectROI;
        private static int[] existCount = new int[3];
        private int detectMethod;
        private string name;
        private Hashtable args;
        private bool isSelected = false;
        public delegate void SetSelectedItem(string itemName, string mode, IElemPack iep = null);
        public delegate void AddMethod(IElemPack elemPack, int aimlist);

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public Hashtable Args
        {
            get { return args; }
            set { args = value; }
        }
        public bool IsSelected
        {
            get { return isSelected; }
        }

        public void BeTouched()
        {
            isSelected = isSelected == false ? true : false;
        }


        public void Dispose()
        {
            if (circle != null) circle.Dispose();
            if (detectROI != null) detectROI.Dispose();
        }
        public CirclePack()
        {

        }
   
        public bool IsDetectType()
        {
            if (detectROI == null) return false;
            else return true; 
        }

        public CirclePack(Circle circle,int detectMethod, Hashtable args,ROI detectROI)
        {
            this.args = new Hashtable();
            this.circle = new Circle(circle);
            this.detectMethod = detectMethod;
            if (args != null)
            {
                IDictionaryEnumerator ie = args.GetEnumerator();
                while (ie.MoveNext())
                    this.args.Add(ie.Key, ie.Value);
            }
            this.detectROI = new ROI(detectROI);
        }

        //public CirclePack(ROI detectROI,int detectMethod, Hashtable args)
        //{
        //    this.args = new Hashtable();
        //    this.detectROI = new ROI(detectROI);
        //    this.detectMethod = detectMethod;
        //    this.circle = null;
        //    IDictionaryEnumerator ie = args.GetEnumerator();
        //    while (ie.MoveNext())
        //    this.args.Add(ie.Key, ie.Value);
        //}
     
        public CirclePack(CirclePack circlePack)
        {
            this.args = new Hashtable();
            this.circle = new Circle(circlePack.circle);
            name = circlePack.name;
            detectMethod = circlePack.detectMethod;
            detectROI = new ROI(circlePack.detectROI);
            //HTuple row, column, area;
            //HOperatorSet.AreaCenter(detectROI.Region, out area, out row, out column);
            if (circlePack.args != null)
            {
                IDictionaryEnumerator ie = circlePack.args.GetEnumerator();
                while (ie.MoveNext())
                    args.Add(ie.Key, ie.Value);
            }

        }

        public void ApplyChange(string mode)
        {
       
            if (detectMethod == ConstValue.MEASURE_CIRCLE)
            {
                name = "圆"+ existCount[0]++; ;

            }
            else if (detectMethod == ConstValue.MEASURE_CIRCLE2)
            {
                name = "峰值圆" + existCount[1]++;

            }
            else if(detectMethod == ConstValue.MEASURE_C_Center)
            {
                existCount[3]++;
            }
            circle.Name = name;

            // circle.ApplyChange();
        }


        public void BeTouched(HTuple windowHandle, SetSelectedItem method)
        {
            isSelected = isSelected == false ? true : false;
            if (isSelected)
            {
                method(name, "add", this);
                DispROI(windowHandle, ConstValue.SELECTED_COLOR);

            }
            else
            {
                method(name, "remove", null);
                DispROI(windowHandle, ConstValue.UNSELECTED_COLOR);
            }
        }

        public void Unload()
        {
            circle.Dispose();
            circle = null;
        }
        public IElem Detect(HObject image, HTuple homMat2D)
        {
                HObject actualRegion;
                HTuple area, row, column;
              

                //MessageBox.Show("circle");
                HOperatorSet.AffineTransRegion(detectROI.Region(), out actualRegion, homMat2D, "nearest_neighbor");
                HOperatorSet.AreaCenter(actualRegion, out area, out row, out column);
                //HOperatorSet.AffineTransContourXld(detectROI.Contours, out actualContour, homMat2D);
                //HOperatorSet.SetColor(Utils.hv_ExpDefaultWinHandle, "green");
                //HOperatorSet.DispObj(actualRegion, Utils.hv_ExpDefaultWinHandle);

                //HOperatorSet.GenRegionContourXld(actualContour, out actualRegion,"filled");
                //HOperatorSet.AreaCenter(actualRegion, out area, out row, out column);

                if (detectMethod == ConstValue.MEASURE_CIRCLE)
                    {
                        HTuple minRadius = (HTuple)args[ConstValue.MIN_RADIUS];
                        HTuple maxRadius = (HTuple)args[ConstValue.MAX_RADIUS];
                        HTuple minThreshold = (HTuple)args[ConstValue.MIN_THRESHOLD];
                        HTuple maxThreshold = (HTuple)args[ConstValue.MAX_THRESHOLD];
        
                        circle = Utils.Get_circle_fromROI(image, actualRegion, Utils.hv_ExpDefaultWinHandle, minRadius, maxRadius, minThreshold, maxThreshold);
                   
                    }
                    else if(detectMethod == ConstValue.MEASURE_CIRCLE2)
                    {
                        MessageBox.Show("需要补全");
                    }
                      if (circle == null) { MessageBox.Show("没有检测到"); }
                    return circle;
             }

            public void Reset(int num = 0)
            {
                for (int i = 0; i < existCount.Length; i++)
                {
                    existCount[i] = 0;
                }
           
            }
            public void Serialization()
            {

            }

            public ROI DetectROI
            {
                get { return detectROI; }
                set { if (detectROI != null) detectROI.Dispose();detectROI = new ROI(value); }
            }
            public int DetectMethod
            {
                get { return detectMethod; }
                set { detectMethod = value; }
            }

            public void DispROI(HTuple windowHandle,string color)
            {
                HOperatorSet.SetLineWidth(windowHandle, 2);
                circle.dispROI(windowHandle,color);
            }



            public HObject Contours()
            {
                if (circle == null) return null; 
                return circle.CircleContours;
            }
            public HObject Region()
            {
                if (circle == null) return null; return circle.Region;
            } 
  
            public Circle C
            {
                get { return circle; }
                set { circle = value; }
            }

     
    }
}

